using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Youshow.Ace.Domain.Models;
using Youshow.Ace.Domain.Repository;

namespace BlogSystem.Domain.AccountInfo
{
    public class User:BaseModel<int>
    {
        [Comment("用户名")]
        [Column("user_name")]
        public string UserName { get; set; }
        
        [Comment("密码")]
        [Column("password")]
        public string Password { get; set; }

        [Comment("性别")]
        [Column("sex")]
        public string? Sex { get; set; } = "保密";

        [Comment("生日")]
        [Column("birthday")]
        public DateTime BirthDay { get; set; }

        [Comment("邮箱")]
        [Column("email")]
        public string? Email { get; set; }

        [Comment("QQ")]
        [Column("qq")]
        public string? QQ { get; set; } 

        [Comment("创建时间")]
        [Column("create_time")]
        public DateTime RegistTime { get; set; }

        public static async Task<User> CheckLogin(string userName, string password, IRepository<User> userRep) //IRepository<User> userRep
        {
            // if(string.IsNullOrEmpty(this.UserName) || string.IsNullOrEmpty(this.Password))
            // {
            //     return null;//"用戶名不能爲空";
            // }
            var user = await userRep.GetAsync(u => u.UserName == userName && u.Password == password);
            return user;
        }

        public void ValidateRegist()
        {

            Regex regexQQ = new Regex(@"^(?:0|(?:-?[1-9]\d*))$");
            if (!regexQQ.IsMatch(this.QQ))
            {
                throw new Exception("QQ格式不正确");
            }
            Regex regexEmail = new Regex("^(([^<>()[\\]\\.,;:\\s@\"]+(\\.[^<>()[\\]\\.,;:\\s@\"]+)*)|(\".+\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$");
            if (!regexEmail.IsMatch(this.Email))
            {
                throw new Exception("Email格式不正确");
            }
            if (this.BirthDay == null)
            {
                var defaultBirthDay = "1900-01-01";
                this.BirthDay = DateTime.Parse(defaultBirthDay);
            }
            this.RegistTime = DateTime.Now;
        }
    }
}